package rhc.taskSchedule.taskDistribute.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import rhc.discribute.node.commander.Commander;
import rhc.discribute.node.host.Host;
import rhc.taskSchedule.commander.TaskScheduleCommanderExecute;
import rhc.taskSchedule.entity.Running;
import rhc.taskSchedule.entity.Task;
import rhc.taskSchedule.entity.TaskType;
import rhc.taskSchedule.exception.NodeException;
import rhc.taskSchedule.exception.TaskException;
import rhc.taskSchedule.exception.TaskTypeException;
import rhc.taskSchedule.taskDistribute.StartRunning;

public class TaskExecuteStartRunning extends BasicStartRunning implements StartRunning{

	@Override
	public void startRunning(Running running, TaskScheduleCommanderExecute taskScheduleCommanderExecute) throws Exception {
		
		String runningId=running.getId();
		String sourceId=running.getSourceId();
		
		List<String> runNodeLabelList=getTaskRunNodeLabelList(sourceId);
		if(null == runNodeLabelList || runNodeLabelList.isEmpty()){
			throw new TaskException("任务 {} 没有相应的运行节点标识",sourceId);
		}
		
		int index=runNodeLabelList.size()==1?0:new Random().nextInt(runNodeLabelList.size());
		
		String runNodeLabel=runNodeLabelList.get(index);
		
		Host runNode=getRunNode(runNodeLabel,taskScheduleCommanderExecute);
		
		this.sendRunnning(runningId, runNode, taskScheduleCommanderExecute);
	}
	
	/**获取该任务类型
	 * @param taskId
	 * @return
	 * @throws Exception
	 */
	private List<String> getTaskRunNodeLabelList(String taskId) throws Exception{
		Task task=superDao.getOneT(new Query(Criteria.where(Task.COLUMN_ID).is(taskId)), Task.class);
		if(null == task){
			throw new TaskException("任务 {} 不存在",taskId);
		}
		String taskTypeId=task.getTaskTypeId();
		
		TaskType taskType=superDao.getOneT(new Query(Criteria.where(TaskType.COLUMN_ID).is(taskTypeId)), TaskType.class);
		if(null == taskType){
			throw new TaskTypeException("任务类型 {} 不存在",taskTypeId);
		}
		List<String> runNodeLabelList=taskType.getRunNodeLabel();
		
		return runNodeLabelList;
	}
	
	/**获取该节点标识的节点
	 * @param runNodeLabel
	 * @param taskScheduleCommander
	 * @return
	 * @throws Exception
	 */
	private Host getRunNode(String runNodeLabel,TaskScheduleCommanderExecute taskScheduleCommanderExecute) throws Exception{
		Commander distributeNodeCommander=taskScheduleCommanderExecute.getDistributeNodeCommander();
		
		Set<Host> nodeSet=distributeNodeCommander.getNodeManage().getLabelHostSet(runNodeLabel);
		if(null == nodeSet || nodeSet.isEmpty()){
			throw new NodeException("节点标签 {} 未找到节点",runNodeLabel);
		}
		if(nodeSet.size() == 1){
			return nodeSet.iterator().next();
		}else{
			List<Host> nodeList=new ArrayList<Host>(nodeSet);
			return nodeList.get(new Random().nextInt(nodeList.size()));
		}
	}

}
